/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.minidota.utils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Random;

/**
 *
 * @author custom
 */
public class Util {

    public static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
    private static final Random rnd = new Random();
    private static String msgSeparator;
    private static String anyLetter = "[A-Za-zА-Яа-яЁё]*";
    private static String censoreReplacement = "...";

    public static int getRandomInt(int n) {
        return rnd.nextInt(n);
    }

    public static int getRandomInt(int fromNumber, int toNumber) {
        return (toNumber - fromNumber >= 0) ? getRandomInt(toNumber - fromNumber + 1) + fromNumber : 0;
    }

    public static double getRandom() {
        return rnd.nextDouble();
    }

    public static double getRandom(double from, double to) {
        return (to - from >= 0) ? rnd.nextDouble() * (to - from) + from : 0;
    }
    public static final double SIN_45 = 1 / Math.sqrt(2);
    /*private static final int SIN[] = {0, 175, 349, 523, 698, 872, 1045, 1219, 1392, 1564,
     1736, 1908, 2079, 2250, 2419, 2588, 2756, 2924, 3090, 3256,
     3420, 3584, 3746, 3907, 4067, 4226, 4384, 4540, 4695, 4848,
     5000, 5150, 5299, 5446, 5592, 5736, 5878, 6018, 6157, 6293,
     6428, 6561, 6691, 6820, 6947, 7071, 7193, 7314, 7431, 7547,
     7660, 7771, 7880, 7986, 8090, 8192, 8290, 8387, 8480, 8572,
     8660, 8746, 8829, 8910, 8988, 9063, 9135, 9205, 9272, 9336,
     9397, 9455, 9511, 9563, 9613, 9659, 9703, 9744, 9781, 9816,
     9848, 9877, 9903, 9925, 9945, 9962, 9976, 9986, 9994, 9998,
     10000};*/
    private static final double SIN[] = {0, 0.0175, 0.0349, 0.0523, 0.0698, 0.0872, 0.1045, 0.1219, 0.1392, 0.1564,
        0.1736, 0.1908, 0.2079, 0.2250, 0.2419, 0.2588, 0.2756, 0.2924, 0.3090, 0.3256,
        0.3420, 0.3584, 0.3746, 0.3907, 0.4067, 0.4226, 0.4384, 0.4540, 0.4695, 0.4848,
        0.5000, 0.5150, 0.5299, 0.5446, 0.5592, 0.5736, 0.5878, 0.6018, 0.6157, 0.6293,
        0.6428, 0.6561, 0.6691, 0.6820, 0.6947, 0.7071, 0.7193, 0.7314, 0.7431, 0.7547,
        0.7660, 0.7771, 0.7880, 0.7986, 0.8090, 0.8192, 0.8290, 0.8387, 0.8480, 0.8572,
        0.8660, 0.8746, 0.8829, 0.8910, 0.8988, 0.9063, 0.9135, 0.9205, 0.9272, 0.9336,
        0.9397, 0.9455, 0.9511, 0.9563, 0.9613, 0.9659, 0.9703, 0.9744, 0.9781, 0.9816,
        0.9848, 0.9877, 0.9903, 0.9925, 0.9945, 0.9962, 0.9976, 0.9986, 0.9994, 0.9998,
        1};

    public static double sin(int a) {
        int angle = fixAngle(a);
        if (angle > 180) {
            return -SIN[angle > 270 ? 360 - angle : angle - 180];// * 0.0001;
        } else {
            return SIN[angle > 90 ? 180 - angle : angle];// * 0.0001;
        }
    }

    public static double cos(int angle) {
        //public static int cos(int angle) {
        return sin(90 - angle);
    }

    public static int fixAngle(int angle) {
        int modAngle = angle % 360;
        if (modAngle < 0) {
            modAngle += 360;
        }
        return modAngle;
    }

    /**
     * Генерирует MD5 hash для исходного текста
     *
     * @param text
     * @return
     */
    public static String Md5Hash(String text) {
        MessageDigest m = null;
        try {
            m = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException ex) {
            System.out.println("NoSuchAlgorithmException: " + ex);
        }
        byte[] data = text.getBytes();
        m.update(data, 0, data.length);
        BigInteger i = new BigInteger(1, m.digest());
        return String.format("%1$032X", i);
    }

    /**
     * replace "<",">"
     *
     * @param s - plain text
     * @return escaped text
     */
    public static String escapeHtml(String s) {
        if (s == null) {
            return "";
        }
        s = s.replace("<", "&lt;");
        s = s.replace(">", "&gt;");
        return s;
    }

    /*public static char[] getCharByCode(int code) {
     if (code >= 0 && code < 0xD800) {
     return Character.toChars(code);
     } else {
     return Character.toChars(0);
     }
     }*/
    public static char[] getCharByCode(int code) {
        //положительные от 0 до 55295 (если больше возврат 55295)
        if (code >= 0) {
            if (code < 0xD800) {
                return Character.toChars(code);
            } else {
                return Character.toChars(0xD7FF);
            }
            //отрицательные от -8191 до -1 (если меньше возврат -8191)
        } else {
            if (code > -0x2000) {
                return Character.toChars(0x10000 + code);
            } else {
                return Character.toChars(0xE001);
            }
        }
    }

    public static String getMsgSeparator() {
        if (msgSeparator == null) {
            StringBuilder sb = new StringBuilder();
            sb.append(Character.toChars(0xE000));
            msgSeparator = sb.toString();
        }
        return msgSeparator;
    }

    public static String censore(String in) {
        String out = in.replaceAll(anyLetter + "[ХхXx]+[УуYy]+[ЕеЁёЙйЯяEeYy]+" + anyLetter, censoreReplacement)
                .replaceAll("[ПпPp]+[ИиIi]+[ДдDd]+[АаAa]+[РрRr]+" + anyLetter, censoreReplacement)
                .replaceAll(anyLetter + "[ПпPp]+[ИиIi]+[ЗзZz]+[ДдDd]+" + anyLetter, censoreReplacement)
                .replaceAll(anyLetter + "[ЕеEe]+[БбBb]+[АаУуИиAaYyIi]+" + anyLetter, censoreReplacement)
                .replaceAll(anyLetter + "[БбBb]+[ЛлLl]+[ЯяAaYy]+" + anyLetter, censoreReplacement);
        return out;
    }

    public static Integer tryParse(String text) {
        try {
            return new Integer(text);
        } catch (NumberFormatException e) {
            return -1;
        }
    }
}
